package com.ztools.pc.service.impl.information;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ztools.common.model.pc.information.Classes;
import com.ztools.common.model.pc.information.Grade;
import com.ztools.common.model.pc.information.Teacher;
import com.ztools.common.util.common.PageUtil;
import com.ztools.pc.mapper.information.ClassesMapper;
import com.ztools.pc.service.information.ClassesService;
import com.ztools.pc.service.information.GradeService;
import com.ztools.pc.service.information.TeacherService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author:一口香菜
 * @Date: 2023/3/17  19:08
 * @Version 1.0
 */
@Service
public class ClassesServiceImpl extends ServiceImpl<ClassesMapper, Classes> implements ClassesService {

    @Resource
    private GradeService gradeService;
    @Resource
    private TeacherService teacherService;

    @Override
    public Page<Classes> findList(PageUtil<Classes> pageUtil) {
        List<OrderItem> orders = new ArrayList<>();
        QueryWrapper<Classes> queryWrapper = new QueryWrapper<Classes>(pageUtil.getParams());
        Page<Classes> pages = new Page<>(pageUtil.getPage(), pageUtil.getSize());
        // 排序数据  StrUtil.toUnderlineCase huTools将驼峰装换为下划线格式
        orders.add(new OrderItem(StrUtil.toUnderlineCase(pageUtil.getSortName()), "asc".equals(pageUtil.getSortOrder())));
        pages.setOrders(orders);
        Page<Classes> page = page(pages, queryWrapper);
        dictionary(page.getRecords());
        return page;
    }

    @Override
    public List<Classes> findByObject(Classes classes) {
        QueryWrapper<Classes> queryWrapper = new QueryWrapper<>(classes);
        return list(queryWrapper);
    }

    @Override
    public boolean add(Classes classes) {
        return save(classes);
    }

    @Override
    public boolean update(Classes classes) {
        Classes byId = getById(classes);
        return updateById(classes);
    }

    @Override
    public boolean delete(Classes classes) {
        Classes byId = getById(classes);
        return removeById(classes);
    }

    /**
     * 字典翻译
     *
     * @param classesList 对象集合
     */
    public void dictionary(List<Classes> classesList) {
        for (Classes classes : classesList) {
            dictionary(classes);
        }
    }

    public void dictionary(Classes classes) {
        if (classes != null) {
            // 班级名称
            if (classes.getGradeId() != null) {
                Grade grade = Grade.builder().id(classes.getGradeId()).build();
                classes.setGradeName(gradeService.findByObject(grade).get(0).getName());
            }
            // 班主任
            if (classes.getDirector() != null) {
                Teacher teacher = Teacher.builder().id(classes.getDirector()).build();
                classes.setDirectorName(teacherService.findByObject(teacher).get(0).getName());
            }
        }
    }

}
